package com.zoshine.zzg.controller;

import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.annotation.DisableLoginCheck;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.PrisonerSettlement;
import com.zoshine.zzg.pojo.pojostatus.PrisonerSettlementStatus;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.PrisonerSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/prisonerSettlement",method = RequestMethod.POST)
public class PrisonerSettlementController {
    private PrisonerSettlementService prisonerSettlementService;
    
    @Autowired
    public void setPrisonerSettlementService(
            PrisonerSettlementService prisonerSettlementService) {
        this.prisonerSettlementService = prisonerSettlementService;
    }

    /**
     * 获取单个结算
     * @param id;
     * @return prisonerSettlement
     */
    @RequestMapping("/getPrisonerSettlement")
    public BaseResponse getPrisonerSettlement(Long id){
        PrisonerSettlement prisonerSettlement = prisonerSettlementService.get(id);
        return new AppResponse(prisonerSettlement);
    }
    /**
     * 获取犯人结算合(分页)
     * @param prisonerSettlement;
     * @return prisonerSettlementList
     */
    @RequestMapping("/pagePrisonerSettlement")
    public BaseResponse pagePrisonerSettlement(PrisonerSettlement prisonerSettlement){
        List<PrisonerSettlement> prisonerSettlementList = prisonerSettlementService.list(prisonerSettlement);
        return new AppResponse(new PageInfo<>(prisonerSettlementList));
    }

    /**
     * 获取犯人结算提交转账
     * @param idStr;
     * @return prisonerSettlementList
     */
    @RequestMapping("/settlementtoflow")
    public BaseResponse settlementtoflow(String idStr){
        prisonerSettlementService.settlementtoflow(idStr);
        return  AppResponse.OK;
    }

    /**
     * 犯人待结算金额
     * @param id 犯人ID
     * @return 待转账金额
     */
    @DisableLoginCheck
    @RequestMapping("getSettlementMoney")
    public BaseResponse getSettlementMoney(Long id){
        if (id == null){
            throw new AppException(ErrorMessage.ACCOUNT_PRISONER_ID_CAN_NOT_NULL);
        }
        PrisonerSettlement prisonerSettlement = new PrisonerSettlement();
        prisonerSettlement.setPrisonsAccount(id);
        prisonerSettlement.setStatus(PrisonerSettlementStatus.AUDITED.getValue());
        List<PrisonerSettlement> prisonerSettlementList = prisonerSettlementService.list(prisonerSettlement);
        Integer money = 0;
        for (PrisonerSettlement temp:prisonerSettlementList){
            money+=temp.getSettlementMoney();
        }
        Map<String,String> map = new HashMap<>();
        map.put("money",money+"");
        return new AppResponse(map);
    }
}
